package com.zhou.common.pub.model;


import com.zhou.common.pub.enums.BaseEnum;
import com.zhou.common.pub.enums.ResponseEnum;
import com.zhou.common.pub.util.MessageUtils;

/**
 * 注意feign 返回的对象如果是 当前类的话，可能会示例化错误，没有提供set
 *
 * @author xianfeng
 * @date 2023/7/25 16:42
 */
public class Response<T> {

    private String code;
    private String msg;
    private T data;

    public Response() {
    }

    public Response(Integer code, String msg) {
        this(code, msg, null);
    }


    public Response(String code, String msg) {
        this(code, msg, null);
    }

    public Response(Integer code, String msg, T data) {
        this.code = code + "";
        this.data = data;
        this.msg = msg;
    }

    public Response(String code, String msg, T data) {
        this.code = code + "";
        this.data = data;
        this.msg = msg;
    }

    public Response(Object code, Object msg, T data) {
        this.code = code.toString();
        this.msg = msg.toString();
        this.data = data;
    }

    public static Response build(BaseEnum baseEnum) {
        return new Response(baseEnum.getCode(), baseEnum.getMsg(), null);
    }

    public static Response success() {
        return new Response(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), null);
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), data);
        return response;
    }

    public static Response error(String code) {
        return new Response(code, code);
    }

    public static Response error() {
        return new Response(ResponseEnum.SYSTEM_ERROR.getCode(), ResponseEnum.SYSTEM_ERROR.getMsg());
    }

    public String getMsg() {

        return MessageUtils.message(code, msg, new Object[]{});
    }

    public Boolean isSuccess() {
        if (code != null && "0".equals(code)) {
            return true;
        }
        return false;
    }


    @Override
    public String toString() {
        return "Response{" +
                "code='" + code + '\'' +
                ", msg='" + getMsg() + '\'' +
                ", data=" + data +
                ", success=" + isSuccess() +
                '}';
    }

    public String getCode() {
        return code;
    }

    public T getData() {
        return data;
    }
}
